﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using UpdaterForm.Tabs;

namespace UpdaterForm
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow 
    {
        public MainWindow()
        {
            InitializeComponent();
            Refresh(null, null);
        }

        private void Refresh(object sender, EventArgs e)
        {
            SupportedApp.Items.Clear();

            foreach (AppData app in Settings.Instance.Data.AppData)
            {
                var appTab = new AppInstance(app);
                var tab = new TabItem
                {
                    Header = app.AppName,
                    Content = appTab
                };
                SupportedApp.Items.Add(tab);
            }

            var newTab = new AddApp();
            newTab.OnAdd += Refresh;
            var addAppTab = new TabItem{
                Header = "New",
                Content = newTab
            };
            SupportedApp.Items.Add(addAppTab);

            SupportedApp.Items.Add(new TabItem {
                Header = "Settings",
                Content = new SettingsTab()
            });
        }

        private void MainForm_Closing(object sender, CancelEventArgs e)
        {
            foreach (var data in Settings.Instance.Data.AppData)
                data.SaveLogs();

            Settings.SaveConf();
        }
    }
}